package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.RoleModel;
import com.example.auth.service.RoleModelService;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;

import java.util.List;

/**
 * @Classname RoleModelServiceImpl
 * @Description TODO
 * @Date 2022/7/29 17:03
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class RoleModelServiceImpl implements RoleModelService {

    @Autowired
    TableService tableService;

    @Override
    public void updateRoleModel(RoleModel roleModel) {
        tableService.updateById(roleModel,RoleModel.class);
    }

    @Override
    public void deleteRoleModelById(Object id) {
        tableService.deleteById(id,RoleModel.class);
    }

    @Override
    public void saveRoleModel(RoleModel roleModel){
        tableService.save(roleModel,RoleModel.class);
    }

    @Override
    public List<RoleModel> getRoleModelList() {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleModel.class);
        List<RoleModel> roleModelList=tableService.selectList(sqlBuilder,RoleModel.class);
        return roleModelList;
    }

    @Override
    public RoleModel getRoleModelByRoleNo(String roleNo) {
        SqlBuilder sqlBuilder = tableService.getSqlBuilder(RoleModel.class);
        sqlBuilder.and("roleno").eq(roleNo);
        RoleModel roleModel=tableService.selectOne(sqlBuilder,RoleModel.class);
        return roleModel;
    }

    @Override
    public RoleModel getRoleModelById(String id) {
        RoleModel roleModel=tableService.getOneById(id,RoleModel.class);
        return roleModel;
    }

    @Override
    public IPage<RoleModel> getRoleModelPage(Integer pageNum, Integer pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleModel.class);
        if(object != null){
            Object rolename=object.get("rolename");
            if(!StringUtils.isEmpty(rolename)){
                sqlBuilder.and("rolename").like(rolename);
            }
            Object roleno=object.get("roleno");
            if(!StringUtils.isEmpty(roleno)){
                sqlBuilder.and("roleno").like(roleno);
            }
            Object remark=object.get("remark");
            if (!StringUtils.isEmpty(remark)) {
                sqlBuilder.and("remark").like(remark);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        IPage<RoleModel> roleModelIPage=tableService.getPage(sqlBuilder,RoleModel.class);
        return roleModelIPage;
    }
}
